package io.nutz.nutzsite.module.acm.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.nutz.nutzsite.module.acm.models.Contest;
import io.nutz.nutzsite.module.acm.services.ContestProblemService;
import io.nutz.nutzsite.module.acm.services.ContestService;
import io.nutz.nutzsite.module.sys.models.Menu;
import io.nutz.nutzsite.module.sys.models.User;
import io.nutz.nutzsite.common.base.Result;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.Lang;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;

import io.nutz.nutzsite.common.utils.DateUtils;
import io.nutz.nutzsite.common.utils.ShiroUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Date;
/**
 * ACM竞赛 信息操作处理
 * 
 * @author lrw
 * @date 2020-02-16
 */
@IocBean
@At("/acm/contest")
public class ContestController {
	private static final Log log = Logs.get();

	@Inject
	private ContestService contestService;
	@Inject
	private ContestProblemService contestProblemService;
	
	@RequiresPermissions("acm:contest:view")
	@At("")
	@Ok("th:/acm/contest/contest.html")
	public void index(HttpServletRequest req) {
		req.setAttribute("now0", DateUtils.getDateTime());
	}

	/**
	 * 查询ACM竞赛列表
	 */
	@RequiresPermissions("acm:contest:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("title") String title,
					   @Param("startTime") Date beginTime,
					   @Param("endTime") Date endTime,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(title)){
			cnd.and("title", "like", "%" + title +"%");
		}
		if(Lang.isNotEmpty(beginTime)){
			cnd.and("startTime",">=", beginTime);
		}
		if(Lang.isNotEmpty(endTime)){
			cnd.and("startTime","<=", endTime);
		}
		return contestService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,null);
	}

	/**
	 * 新增ACM竞赛
	 */
	@At("/add")
	@Ok("th:/acm/contest/add.html")
	public void add( HttpServletRequest req) {

	}
	@At("/add/?")
    @Ok("th:/acm/contestProblem/add.html")
    public void add(@Param("id") String id, HttpServletRequest req) {
		Contest contest=contestService.fetch(Integer.valueOf(id));
        req.setAttribute("contest", contest);
    }
	/**
	 * 新增保存ACM竞赛
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:contest:add")
	@Slog(tag="ACM竞赛", after="新增保存ACM竞赛 id=${args[0].id}")
	public Object addDo(@Param("..") Contest contest,HttpServletRequest req) {
		try {
			User user = ShiroUtils.getSysUser();
			contest.setCreateUser(user.getId());
			contest.setCreateTime(new Date());
			contest.setType(0);//0:acm/ICPC
			contestService.insert(contest);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 修改ACM竞赛
	 */
	@At("/edit/?")
	@Ok("th://acm/contest/edit.html")
	public void edit(Integer id, HttpServletRequest req) {
		Contest contest = contestService.fetch(id);
		req.setAttribute("contest",contest);
	}

	/**
	 * 修改保存ACM竞赛
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("acm:contest:edit")
	@Slog(tag="ACM竞赛", after="修改保存ACM竞赛")
	public Object editDo(@Param("..") Contest contest,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(contest)){

				contestService.update(contest);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除ACM竞赛
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("acm:contest:remove")
	@Slog(tag ="ACM竞赛", after= "删除ACM竞赛:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			contestProblemService.delete(Cnd.where("contestId","in",ids));
			contestService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
